/*
 * Powered By [Frank-Liz-Lee]
 * Copyright(C) 2012-2016 Liz Company
 * All rights reserved.
 */

package com.linln.modules.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linln.common.constant.StatusConst;
import com.linln.common.enums.StatusEnum;
import com.linln.modules.system.mapper.MenuMapper;
import com.linln.modules.system.domain.Menu;
import java.util.List;

import com.linln.modules.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Transactional(rollbackFor = Exception.class)
@Service
public class MenuServiceImpl  extends GenericService<Menu, Long>  implements MenuService {

    private MenuMapper menuMapper;

    @Autowired
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    /**
     * 通过主键查询实体对象
     * @param primaryKey
     * @return
     * @
     */
    public Menu getByPK(Long primaryKey)  {
        return menuMapper.getByPK(primaryKey);
    }

    /**
     * 查询所有记录
     * @return
     * @
     */
    public List<Menu> list()  {
        return menuMapper.list();
    }

    /**
     * 根据查询条件查询所有记录
     * @return
     * @
     */
    public List<Menu> listByProperty(Menu menu) {
        return menuMapper.listByProperty(menu);
    }


    /**
     * 根据主键删除记录
     * @param primaryKey
     * @return
     * @
     */
    public int deleteByPK(Long primaryKey)  {
        return menuMapper.deleteByPK(primaryKey);
    }

    /**
     * 根据多个主键删除记录
     * @param primaryKeys
     * @
     */
    public int deleteByPKeys(List<Long> primaryKeys)  {
        return menuMapper.deleteByPKeys(primaryKeys);
    }

    /**
     * 根据传入参数删除记录
     * @param menu
     * @return
     * @
     */
    public int deleteByProperty(Menu menu)  {
        return menuMapper.deleteByProperty(menu);
    }

    /**
     * 保存记录
     * @param menu
     * @return
     * @
     */
    public int save(Menu menu)  {
        return menuMapper.save(menu);
    }

    /**
     * 更新记录
     * @param menu
     * @return
     * @
     */
    public int update(Menu menu) {
        return menuMapper.update(menu);
    }

    /**
     * 根据条件查询记录条数
     * @param menu
     * @return
     * @
     */
    public int findByCount(Menu menu)  {
        return menuMapper.findByCount(menu);
    }

    /**
     * 根据查询条件查询分页记录
     * @return
     */
    public PageInfo<Menu> findByPage(Integer pageNum, Integer pageSize, Menu menu) {
        PageHelper.startPage(pageNum,pageSize);
        List<Menu> menuList = menuMapper.listByProperty(menu);
        if(null == menuList || menuList.size() == 0){
            return new Page<Menu>().toPageInfo();
        }
        Page<Menu> page =  (Page<Menu>)menuList;
            return page.toPageInfo();
    }

    @Override
    public List<Menu> getListBySortOk() {
        Menu temp = new Menu();
        temp.setStatus(StatusEnum.OK.getCode());
        return menuMapper.listByProperty(temp);
    }

    @Override
    public List<Menu> getListByPid(Long pid, Long notId) {
        Menu temp = new Menu();
        temp.setPid(pid);
        return menuMapper.listByProperty(temp);
    }

    @Override
    public Integer getSortMax(Long pid) {
        return menuMapper.getSortMax(pid, StatusConst.DELETE);
    }

    @Override
    public boolean updateStatus(StatusEnum statusEnum, List<Long> ids) {
        int result =  menuMapper.updateStatus(statusEnum.getCode(),ids);
        return result > 0;
    }

    /**
     * 根据菜单对象的Example判断是否存在
     * @param menu
     * @return
     */
    @Override
    public Menu getByMenuToExample(Menu menu) {
        List<Menu> menuList = menuMapper.listByProperty(menu);
        return CollectionUtils.isEmpty(menuList) ? null :menuList.get(0);
    }
}
